#include "bsp.h"

#define UARTLITE_BASEADDR XPAR_AXI_UARTLITE_0_BASEADDR

u32 ret32;
u8 ret8;
u8 UserInput;
u8 cerrent_ch;
u8 i2c_device_addr_984;
u8 fake_984_i2c_addr;
u8 lock_984;
u8 lock_988;
u8 clear_flag;
u8 reconfig_flag_984;
u8 wave_flag;


u8 fresh_cnt = 0;
u8 state_cnt = 0;
u8 video_freq = 0;

void app_info(void)
{
	rs485_output();
	xil_printf("\r\nstart\r\n");
	xil_printf("\r\n");
	xil_printf("----------------------\r\n");
	xil_printf("----------------------\r\n");
	print("input :\n\r");
	xil_printf("r - reset board\r\n");
	xil_printf("d - detect info\r\n");
	xil_printf("c - clear screen\r\n");
#ifdef XPAR_XAXIS_SWITCH_NUM_INSTANCES
	xil_printf("s - switch video source\r\n");
#endif
	xil_printf("----------------------\r\n");
	xil_printf("\r\n");
	rs485_input();
}

void video_resolution_print(char *info,u32 baseaddr)
{
	rs485_output();

	xil_printf("-%s freq: %d -\r\n", info, AXI_PASSTHROUTH_MONITOR_REG_mReadReg(baseaddr, S_AXI_PASSTHROUTH_MONITOR_REG2_OFFSET));
	xil_printf("-%s res: %dx%d -\r\n", info, AXI_PASSTHROUTH_MONITOR_REG_mReadReg(baseaddr, S_AXI_PASSTHROUTH_MONITOR_REG0_OFFSET),\
												AXI_PASSTHROUTH_MONITOR_REG_mReadReg(baseaddr, S_AXI_PASSTHROUTH_MONITOR_REG1_OFFSET));
	rs485_input();
}

void uart_receive_process(void)
{
	rs485_input();
	if (!XUartLite_IsReceiveEmpty(UARTLITE_BASEADDR))
	{
		// Read data from uart
		UserInput = XUartLite_RecvByte(UARTLITE_BASEADDR);
		if((UserInput == 'm') || (UserInput == 'M'))
		{
			app_info();
		}
		else if((UserInput == 'd') || (UserInput == 'D'))
		{
			rs485_output();
			xil_printf("\r\n!!!!!!!!!!!!!!!!!!!!!!!!\r\n");
			xil_printf("------------------------\r\n");
			xil_printf("------------cerrent_ch is %d----------\r\n",cerrent_ch);
			video_resolution_print("tpg out",XPAR_AXIS_PASSTHROUGH_MON_TPG_OUT_S_AXI_LITE_BASEADDR);
			video_resolution_print("sd card vdma output",XPAR_AXIS_PASSTHROUGH_MON_VDMA_OUT_S_AXI_LITE_BASEADDR);
			video_resolution_print("hdmi input",XPAR_AXIS_PASSTHROUGH_MON_HDMI_IN_S_AXI_LITE_BASEADDR);
			video_resolution_print("vid in, before csc",XPAR_AXIS_PASSTHROUGH_MON_VID_IN_S_AXI_LITE_BASEADDR);
			video_resolution_print("video out 0",XPAR_AXIS_PASSTHROUGH_MON_LVL2_0_S_AXI_LITE_BASEADDR);
			video_resolution_print("video out 1",XPAR_AXIS_PASSTHROUGH_MON_LVL2_1_S_AXI_LITE_BASEADDR);
			video_resolution_print("video out 2",XPAR_AXIS_PASSTHROUGH_MON_LVL2_2_S_AXI_LITE_BASEADDR);
			video_resolution_print("video out 3",XPAR_AXIS_PASSTHROUGH_MON_LVL2_3_S_AXI_LITE_BASEADDR);
			xil_printf("------------------------\r\n");
			rs485_input();
		}
		else if((UserInput == 'r') || (UserInput == 'R'))
		{
			rs485_output();
			xil_printf("------------reset board------------\r\n");
			XGpioPs_WritePin(&Gpio, RELAY_CUTOFF, 1) ; // RELAY_CUTOFF: 0-keep; 1-cutoff
			rs485_input();
		}
		else if((UserInput == 'c') || (UserInput == 'C'))
		{
			rs485_output();
			xil_printf("------------clear vdma start------------\r\n");
			clear_display();
			rs485_input();
		}
		else if((UserInput == 's') || (UserInput == 'S'))
		{
			rs485_output();
			xil_printf("switch video source\r\n");
			xil_printf("0 - sd card\r\n");
			xil_printf("1 - tpg\r\n");
			xil_printf("2 - hdmi in\r\n");
			rs485_input();

			UserInput = XUartLite_RecvByte(UARTLITE_BASEADDR);

			rs485_output();
			xil_printf("------------------------\r\n");
			if(UserInput == '0')
			{
				axis_switch_cfg(0);
			    xil_printf("------------switch to sd card-----------\r\n");
			}
			else if(UserInput == '1')
			{
				axis_switch_cfg(1);
				xil_printf("------------switch to tpg-----------\r\n");
			}
			else if(UserInput == '2')
			{
				axis_switch_cfg(2);
				xil_printf("------------switch to hdmi in-----------\r\n");
			}
			else
			{
				xil_printf("\r input data error \n!!!!!!!!!!!!!");
			}

			xil_printf("------------------------\r\n");
			rs485_input();
		}
	}
}

#if	defined (MODBUS_RTU_SLAVE)
void MODS_Switch(void)
{
	u8 data;
	data = (u8)g_tVar.P01;
	if((data >=1) && (data <= 3))
	{
		g_tVar.P01 = 0;
		if((data == 1) && (data != cerrent_ch))
		{
			cerrent_ch = 1;
			AxisSwitch(XPAR_MEMORY_SUBSYSTEM_AXIS_SWITCH_0_DEVICE_ID, &AxisSwitch2, 0, 0); //stream
			AxisSwitch(XPAR_AXIS_SWITCH_0_DEVICE_ID, &AxisSwitch1, 0, 0); //A
			clear_vdma_0();
			bsp_printf("MODS_Switch to A screen succeeded\r\n");

		}
		else if((data == 2) && (data != cerrent_ch))
		{
			AxisSwitch(XPAR_MEMORY_SUBSYSTEM_AXIS_SWITCH_0_DEVICE_ID, &AxisSwitch2, 0, 0); //stream
			cerrent_ch = 2;
			XGpio_DiscreteWrite(&XGpioOutput_oldi, 1, 0x1);   //reset
			usleep(1000);
			XGpio_DiscreteWrite(&XGpioOutput_oldi, 1, 0x0);   //B
			AxisSwitch(XPAR_AXIS_SWITCH_0_DEVICE_ID, &AxisSwitch1, 1, 0);
			clear_vdma_0();
			bsp_printf("MODS_Switch to B screen succeeded\r\n");
		}
		else if((data == 3) && (data != cerrent_ch))
		{
			AxisSwitch(XPAR_MEMORY_SUBSYSTEM_AXIS_SWITCH_0_DEVICE_ID, &AxisSwitch2, 0, 0); //stream
			cerrent_ch = 3;
			XGpio_DiscreteWrite(&XGpioOutput_oldi, 1, 0x5);   //reset
			usleep(1000);
			XGpio_DiscreteWrite(&XGpioOutput_oldi, 1, 0x4);   //C
			AxisSwitch(XPAR_AXIS_SWITCH_0_DEVICE_ID, &AxisSwitch1, 1, 0);
			clear_vdma_0();
			bsp_printf("MODS_Switch to C screen succeeded\r\n");
		}
	}
	else if(data == 4)
	{
		g_tVar.P01 = 0;
		XGpio_DiscreteWrite(&XGpioOutput_oldi, 1, (XGpio_DiscreteRead(&XGpioOutput_oldi,1) | 0x01));
		usleep(1*1000);
		XGpio_DiscreteWrite(&XGpioOutput_oldi, 1, (XGpio_DiscreteRead(&XGpioOutput_oldi,1) & ~0x01));
		bsp_printf("rest native succeeded\r\n");

	}
	else if(data == 5)
	{
		g_tVar.P01 = 0;
		clear_vdma_0();
		bsp_printf("rest VDMA succeeded\r\n");

	}
	else if(data == 6)
	{
		g_tVar.P01 = 0;
		xil_printf("------------power off-reboot------------\r\n");
    	ret32 = XGpio_DiscreteRead(&Axi_Gpio,1);
		STB(ret32, BIT32(21));
		XGpio_DiscreteWrite(&Axi_Gpio, 1,ret32);
	}
}

#endif
